#!/usr/bin/env python
import time, os
MAX=10
INTERVAL=0.0125000
FG="X"
BG=" "

class Hanoi:
    def __init__( self, size ):
        a = range( size, 0, -1 )
        b = []
        c = []
        self.pegs = [ a, b, c ]
        max_width=MAX*2+1
        self.disks = []
        for disk in range(0,MAX+1):
            disk_str=""
            st=max_width/2-disk
            en=max_width/2+disk
            for i in range( 0, max_width ):
                if i > st and i < en:
                    disk_str+=FG
                else:
                    disk_str+=BG
            self.disks.append(disk_str)
            
    def run(self):
        self.print_pegs()
        while True:
            time.sleep(1)
            self.hanoi( len(self.pegs[0]), 0, 1, 2 )
            time.sleep(1)
            self.hanoi( len(self.pegs[1]), 1, 2, 0 )
            time.sleep(1)
            self.hanoi( len(self.pegs[2]), 2, 0, 1 )

    def hanoi( self, n, f, t, u ):
        if n == 1:
            self.pegs[t].append( self.pegs[f].pop() )
            self.print_pegs()
        else:
            self.hanoi( n-1, f, u, t )
            self.hanoi( 1, f, t, u )
            self.hanoi( n-1, u, t, f )

    def print_pegs(self ):
        os.system("clear")
        for tier in range(MAX,-1,-1):
            line=""
            for peg in self.pegs:
                if len(peg) > tier:
                    line += self.disks[peg[tier]]
                else:
                    line += self.disks[0]
            print line
        time.sleep(INTERVAL)

h=Hanoi(10)
h.run()
